package com.maoye.ossp.common.web.taglib;

import java.io.IOException;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.maoye.sc.api.domain.User;
import com.maoye.sc.api.local.UserService;

/**
 * 通过用户ID查询用户名称
 * 
 * @author zxpfss
 * 
 */
public class UserNameTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 1L;
	/**
	 * 用户主键
	 */
	private String userId;

	private UserService<User> userService;

	@Override
	protected int doStartTagInternal() throws Exception {
		try {
			User worker = getUserService().getUserById(userId);
			if (worker != null) {
				String name = worker.getName();
				pageContext.getOut().print(name);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	// -------------------get/set----------------------

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public UserService getUserService() {
		if (this.userService == null) {
			this.userService = (UserService) getRequestContext()
					.getWebApplicationContext().getBean("workerServiceImpl");
		}
		return userService;
	}

}
